﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudApiServer.Services
{
    public class FsiIntegratedQueryService
    {

        /// <summary>
        /// 5301人员慢特病查询
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="CountrollerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<OutputQueryPsnOpspReg> QueryPsnOpspReg(FsiRequestHeader<InputQueryPsnOpspReg> fsiRequest, string CountrollerName)
        {
            string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
            string psn_no = fsiRequest.Input.data.psn_no;
            if (string.IsNullOrEmpty(psn_no))
            {
                throw new Exception("参数校验失败：传入的人员编号psn_no为空，请检查入参！");
            }
            string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            using (var conn = new MySqlConnection(connectionString))
            {
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                await conn.OpenAsync();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from fsi_querypsnopspreg where psn_no=@psn_no";
                    cmd.Parameters.AddWithValue("@psn_no", psn_no);
                    using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                    {
                        DataTable dt = new DataTable();
                        List<FeedetailQueryPsnOpspReg> dataList = new List<FeedetailQueryPsnOpspReg>();
                        dt.Load(reader);
                        cmd.Parameters.Clear();
                        foreach (DataRow row in dt.Rows)
                        {
                            FeedetailQueryPsnOpspReg item = new FeedetailQueryPsnOpspReg
                            {
                                opsp_dise_code = row["opsp_dise_code"].ToString(),
                                opsp_dise_name = row["opsp_dise_name"].ToString(),
                                exp_content = row["exp_content"].ToString(),
                                begndate = row["begndate"].ToString(),
                                enddate = row["enddate"].ToString(),

                            };
                            dataList.Add(item);
                        }
                        var output = new OutputQueryPsnOpspReg
                        {
                            feedetail = dataList,
                        };
                        return output;
                    }
                }
            }
        }
        /// <summary>
        /// 5206年度费用累计
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="CountrollerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<outputCuminfo> QueryFixmedinsPracPsnSum(FsiRequestHeader<InputQueryFixmedinsPracPsnSum> fsiRequest, string CountrollerName)
        {
            string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
            string psn_no = fsiRequest.Input.data.psn_no;
            if (string.IsNullOrEmpty(psn_no))
            {
                throw new Exception("参数校验失败：传入的人员编号psn_no为空，请检查入参！");
            }
            string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            using (var conn = new MySqlConnection(connectionString))
            {
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                await conn.OpenAsync();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from fsi_queryfixmedinspracpsnsum where psn_no=@psn_no";
                    cmd.Parameters.AddWithValue("@psn_no", psn_no);
                    using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                    {
                        DataTable dt = new DataTable();
                        dt.Load(reader);
                        List<Cuminfo> dataList = new List<Cuminfo>();
                        foreach (DataRow row in dt.Rows)
                        {
                            Cuminfo item = new Cuminfo
                            {
                                cum_type_code = row["cum_type_code"].ToString(),
                                year = row["year"].ToString(),
                                cum = (decimal)row["cum"],
                                cum_ym = row["cum_ym"].ToString(),
                                insutype = row["insutype"].ToString()
                            };
                            dataList.Add(item);
                        }
                        var output = new outputCuminfo
                        {
                            cuminfo = dataList
                        };
                        return output;
                    }
                }
            }
        }
        
        
        
    }
}
